package com.gitee.huanminabc.db_freedom.action;

import com.gitee.huanminabc.db_freedom.proxy.ActionProxy;
import com.gitee.huanminabc.db_freedom.base.SqlHelper;
import com.gitee.huanminabc.db_freedom.wrapper.AbstractSqlWrapper;
import com.gitee.huanminabc.db_freedom.wrapper.LambdaSQL;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;

/**
 * 打印执行的sql  目前只针对SqlWrapperHelper的操作可以打印
 * 而mybatis的操作有自己的日志打印,所以这里就不在打印了
 */
@Slf4j
public class SqlPrintRecordAction implements ActionProxy {
    public static Boolean isPrint = true;//是否打印sql, 默认打印可以自行设置为false
    @Override
    public void before(Object target, Method method, Object[] args) {
        if (!isPrint){
            return;
        }
        if (target instanceof SqlHelper){
            //检测第一个参数是否是 LambdaSQL
            if (args[0] instanceof LambdaSQL){ //单个操作
                AbstractSqlWrapper sqlWrapper = (AbstractSqlWrapper) args[0];
                log.info("before.{}: \n{}", method.getName(), sqlWrapper);
            }else if ( args[0] instanceof LambdaSQL[]){ //批量操作
                AbstractSqlWrapper[] sqlWrappers = (AbstractSqlWrapper[]) args[0];
                for (AbstractSqlWrapper sqlWrapper : sqlWrappers) {
                    log.info("before.{}: \n{}", method.getName(), sqlWrapper);
                }
            }
        }

    }
}
